﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using NLog;

namespace Tab2Sql.Classes.Core
{
    /// <summary>
    /// Primary class for Tab2Sql. 
    /// </summary>
    public class MainTab2Sql
    {
        /// <summary>
        /// Holds the xTab2Sql object. 
        /// </summary>
        private xTab2Sql xTS;
       
        private xDataTable2Sql xDT;

		private static Logger logger = LogManager.GetCurrentClassLogger ();
		
        /// <summary>
        /// Flag recording that table has been instantiated. 
        /// </summary>
        private bool hasRun;
        /// <summary>
        /// As the name suggests, this holds the various options available.
        /// </summary>
        private Options options;

        /// <summary>
        /// Constructor: Takes an inputobject and creates objects. 
        /// Call the Go method to fill the table.
        /// </summary>
        /// <param name="io"></param>
        public MainTab2Sql (InputObject io)
        {
            logger.Trace ("Constructor called. io variant.");

            hasRun = false;
            options = io.TheOptions;
            try
            {
                xTS = new xTab2Sql (io.InputString, options);
            }
			catch (Exception ex)
            {
                logger.Error (ex.Message);
            }
   
            
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="inputString">
        /// A <see cref="System.String"/>
        /// </param>
        /// <param name="theOptions">
        /// A <see cref="Options"/>
        /// </param>
		public MainTab2Sql (string inputString, Options theOptions)
		{
            logger.Trace ("Constructor called. inputString/theOptions variant.");
			hasRun = false;
			options = theOptions;
			xTS = new xTab2Sql (inputString, options);
			xTS.Process ();
			xDT = new xDataTable2Sql (xTS);
			hasRun = true;
		}
		
		/// <summary>
		/// Do it faggot 
		/// </summary>
        public void Go()
        {
            xTS.Process();
            xDT = new xDataTable2Sql(xTS);
            hasRun = true;
        }

        

        /// <summary>
        /// The entire Sql Statement in full
        /// </summary>
        public string FullSql
        {
            get
            {
                if (hasRun == true) { return xDT.FullStatement; }
                else { return "No statement!"; }
            }
        }
        /// <summary>
        /// The create table statement alone.
        /// </summary>
        public string CreateTable
        {
            get
            {
                if (hasRun == true) { return xDT.CreateTable().ToString(); }
                else { return "No statement!"; }
            }
        }

        /// <summary>
        /// The underlying datatable
        /// </summary>
        public DataTable OurDataTable { get { return xTS.OurDataTable; } }

    }
}
